/*
 *  linmigrate - command line parser for linmigrate
 *
 *  Copyright (C) 2011 by Volodymyr M. Shcherbyna <volodymyr@shcherbyna.com>
 *
 *      This file is part of linmigrate.
 *
 *  linmigrate is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  linmigrate is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with linmigrate.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "cmdparser.h"

/**   C'tor of the object

    * @return initialized object
*/
CCmdParser::CCmdParser(const QStringList & arguments) : m_Command(CmdUnknown), m_Arguments(arguments)
{

#define IF_COMMAND_ALREADY_FOUND if (m_Command != CmdUnknown)\
                                 {                              \
                                    m_Command = CmdUnknown; \
                                    break;\
                                 }

    for (int i = 0; i < arguments.size(); i++)
    {
        if (arguments[i] == CMD_PARSER_COMMAND_SZ_HELP)
        {
            IF_COMMAND_ALREADY_FOUND

            m_Command = CmdHelp;
        }
        else if (arguments[i] == CMD_PARSER_COMMAND_SZ_EXPORT)
        {
            IF_COMMAND_ALREADY_FOUND

            m_Command = CmdExport;
        }
        else if (arguments[i] == CMD_PARSER_COMMAND_SZ_IMPORT)
        {
            IF_COMMAND_ALREADY_FOUND

            m_Command = CmdImport;
        }
        else
        {
            insert(arguments[i], arguments[i]);
        }
    }
}

/**   Getting the command user has specified

    * @return CmdParserCommand enumerator
*/
CCmdParser::CmdParserCommand CCmdParser::getCommand(void)
{
    return m_Command;
}

/**   Getting the options for export command

    * @return ExportOptions struct
*/
ExportOptions CCmdParser::getExportOptions(void)
{
    ExportOptions retVal;

    for (QMap<QString, QString>::iterator it = this->begin(); it != this->end(); it++)
    {
        if (it.key() == CMD_PARSER_COMMAND_SZ_EXPORT_OPTION_SYMLINK_EXCLUDE)
        {
            retVal.bExcludeSymlinks = true;
        }
    }

    return retVal;
}

/**   Getting the options for import command

    * @return ImportOptions struct
*/
ImportOptions CCmdParser::getImportOptions(void)
{
    ImportOptions retVal;

    for (QStringList::const_iterator it = m_Arguments.begin(); it != m_Arguments.end(); it++)
    {
        if (*it == CMD_PARSER_COMMAND_SZ_IMPORT_OPTION_PATH)
        {
            QStringList::const_iterator it_next = it;

            it_next++;

            if (it_next != m_Arguments.end())
            {
                retVal.szImportFile = *it_next;
            }
        }
    }

    return retVal;
}
